package com.example.hengjun.operation_oracle.controller;

import com.example.hengjun.operation_oracle.entity.Testuser;
import com.example.hengjun.operation_oracle.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

//swagger地址: http://localhost:8080/swagger-ui.html
@RestController
@RequestMapping("oracle")
@Api("用户管理接口, 增删改查")
public class UserController {

    @Resource
    private UserService userService;


    @ApiOperation("插入数据")
    @PostMapping("addUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "100"),
            @ApiImplicitParam(name = "age", value = "年龄", defaultValue = "18"),
            @ApiImplicitParam(name = "name", value = "用户名", defaultValue = "hhj", required = true)
    })
    public Object addUser(@RequestBody Testuser user, HttpServletRequest request) {
        int add_status = userService.addUser(user);
        return add_status;
    }

    @ApiOperation("根据id来删除某一条数据")
    @GetMapping("delete/{id}")
    @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "10", required = true)
    public Object deleeUser(@PathVariable("id")Integer id)
    {
        int delete_stauts = userService.deleteUser(id);
        return delete_stauts;
    }

    @ApiOperation("根据name来查数据")
    @GetMapping("find/{name}")
    @ApiImplicitParam(name = "name", value = "用户name", defaultValue = "hhj",required = true)
    public Object findUByName(@PathVariable("name") String name) {
        Testuser user = userService.findByName(name);

        return user;
    }


    @ApiOperation("更新用户")
    @PostMapping("update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "100"),
            @ApiImplicitParam(name = "age", value = "年龄", defaultValue = "18"),
            @ApiImplicitParam(name = "name", value = "用户名", defaultValue = "hhj", required = true)
    })
    public Object updateUser(@RequestBody Testuser user, HttpServletRequest request) {
        int update_status = userService.update(user);
        return update_status;
    }
}